package com.xbfinal.eduservice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbfinal.commonUtils.Result;
import com.xbfinal.eduservice.entity.EduSubject;
import com.xbfinal.eduservice.entity.excel.subjectData;
import com.xbfinal.eduservice.entity.subject.OneSubject;
import com.xbfinal.eduservice.entity.subject.TwoSubject;
import com.xbfinal.eduservice.listener.SubjectReadListener;
import com.xbfinal.eduservice.mapper.EduSubjectMapper;
import com.xbfinal.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author xbfinal
 * @since 2023-01-05
 */
@Service
@Slf4j
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public Result saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        String name = file.getOriginalFilename();
        String substring = name.substring(name.lastIndexOf(".") + 1);

        log.info("========>"+name);
        log.info("========>"+substring);
        boolean xlsx = StrUtil.equals(substring, "xlsx");
        log.info("========>"+xlsx);
        if (!xlsx){
            log.info("添加文件失败");
            return Result.failed().addMessage("文件类型必须是 .xlsx").addCode(20001);
        }

        try {
            //文件输入流
            InputStream in = file.getInputStream();
            EasyExcel.read(in, subjectData.class,new SubjectReadListener(eduSubjectService))
                     .sheet()
                     .doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

        log.info("添加文件成功！！");
        return Result.ok();

    }

    @Override
    public List<OneSubject> getAllOneAndTwoSub() {

        //需要返回数据 需要查询所有的一级分类
        QueryWrapper<EduSubject> WrapperOne = new QueryWrapper<>();
            //设置条件
        WrapperOne.eq("parent_id","0");
        List<EduSubject> oneList = this.list(WrapperOne);


        //查询所有的二级分类
        QueryWrapper<EduSubject> WrapperTwo = new QueryWrapper<>();
        WrapperTwo.ne("parent_id","0");
        List<EduSubject> twoList = this.list(WrapperTwo);

        //最终的分类
        List<OneSubject>finalList=new ArrayList<>();

        //封装一级分类
        for (int i = 0; i < oneList.size(); i++) {

            EduSubject eduSubject = oneList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject, oneSubject);


            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            //遍历二级分类list集合

            for (int m = 0; m < twoList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoList.get(m);
                //判断二级分类parentid和一级分类id是否一样
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    //把tSubject值复制到TwoSubject里面，放到twoFinalSubjectList里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
                //把一级下面所有二级分类放到一级分类里面
                oneSubject.setChildren(twoFinalSubjectList);
            }
            finalList.add(oneSubject);

        }

        return  finalList;
    }
}
